<?php
$app_config = require(dirname(__FILE__) . '/../config/boot.php');
require $app_config['QEEPHP_DIR'] . '/library/q.php';
require $app_config['APP_DIR'] . '/myapp.php';
$ret = MyApp::instance($app_config);

$listings = Online_Ebay_Listings::find('online_ebay_listings.execute_flag=? and online_ebay_listings.execute_status=?', Q::ini('custom_flag/online_ebay_listings_execute_flag/opened/value'), Q::ini('custom_flag/online_ebay_listings_execute_status/unexecuted/value'))->order('schedule_time asc')
	->joinLeft('currency', 'code as currency_code', 'currency.id=online_ebay_listings.currency_id')
	->asArray()->getOne();
if ($listings && strtotime($listings['schedule_time']) <= strtotime('now'))
{
	$result = array('ack' => FAILURE, 'message' => '');
	if ($listings['status'] == Q::ini('custom_flag/online_ebay_listings_status/non_add/value'))
	{
		$listings['shipping']['domestic'] = Online_Ebay_Listings_Shipping::find('online_ebay_listings_id=? and type=?', $listings['id'], Q::ini('custom_flag/online_ebay_listings_shipping_type/domestic/value'))->asArray()->getAll();
		$listings['shipping']['internation'] = Online_Ebay_Listings_Shipping::find('online_ebay_listings_id=? and type=?', $listings['id'], Q::ini('custom_flag/online_ebay_listings_shipping_type/internation/value'))->asArray()->getAll();
		$listings['specific'] = Online_Ebay_Listings_Specific::find('type=? and relate_table_id=?', Q::ini('custom_flag/online_ebay_listings_specific_type/listings/value'), $listings['id'])->asArray()->getAll();
		if ($listings['variation'] == Q::ini('custom_flag/online_ebay_listings_variation/variation/value'))
		{
			Online_Ebay_Listings_Picture::getListingsPicture($listings);
			Online_Ebay_Listings_Item::getItemAndSpecific($listings);
			$result = Online_Ebay_Listings::addVariationItem($listings);
		}
		else
		{
			$listings['picture']['listings'] = Online_Ebay_Listings_Picture::find('online_ebay_listings_id=? and type=?', $listings['id'], Q::ini('custom_flag/online_ebay_listings_picture_type/listings/value'))->asArray()->getAll();
			$listings['picture']['gallery'] = Online_Ebay_Listings_Picture::find('online_ebay_listings_id=? and type=?', $listings['id'], Q::ini('custom_flag/online_ebay_listings_picture_type/gallery/value'))->asArray()->getOne();
			$listings['item'] = Online_Ebay_Listings_Item::find('online_ebay_listings_id=?', $listings['id'])->asArray()->getAll();
			$result = Online_Ebay_Listings::addItem($listings);
		}
	}
	$status = ($result['ack'] == SUCCESS ? Q::ini('custom_flag/online_ebay_listings_execute_status/success/value') : Q::ini('custom_flag/online_ebay_listings_execute_status/failure/value'));
	QDB::getConn()->execute('update online_ebay_listings set execute_time=\'' . CURRENT_DATETIME . '\', execute_status=' . $status . ' where id=' . $listings['id']);		
}